package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 通过反射机制调用方法
 */
public class ReflectDemo4 {
    public static void main(String[] args) throws Exception {

        /*
        //实例化Class类对象
        Class cls = Class.forName("reflect.Student");
        //调用无参构造方法实例化Person对象，相当于Object obj = new Person();
        Object obj = cls.newInstance();
        //3.通过类对象获取sayHello方法
        Method method = cls.getMethod("sayHello");
        //4.通过方法对象的invoke方法调用sayHello方法,闯入的参数为所属对象，相当于obj.sayHello();
        method.invoke(obj);

         */
        Scanner scan =new Scanner(System.in);
        System.out.println("请输入类名:");
        String className = scan.nextLine();
        System.out.println("请输入方法名:");
        String methodName = scan.nextLine();

        Class cls = Class.forName(className);
        Constructor c = cls.getConstructor(String.class);
        Object obj = c.newInstance("sjh");
        Method method = cls.getMethod(methodName);
        method.invoke(obj);



    }
}
